﻿using B.S.BasicData.Api.Read.Application.Command.ShenPi;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domian.Entities.Account;
using B.S.BasicData.Domian.Entities.RBAC;
using B.S.BasicData.Domian.Entities.shenpi;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Security.Cryptography;
using System.Xml.Linq;

namespace B.S.BasicData.Api.Read.Application.Handler.ShenPi
{
    public class GetApprovalHandler : IRequestHandler<GetApprovalCommand, ApiPaging<ApprovalDTO>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;
        private readonly IBaseRepository<DeptModel> deptRepo;
        private readonly IBaseRepository<LeaveModel> leaveRepo;
        private readonly IBaseRepository<RoleModel> roleRepo;
        private readonly IBaseRepository<UserRoleModel> userroleRepo;

        public GetApprovalHandler(IBaseRepository<AccountModel>accountRepo,IBaseRepository<DeptModel>deptRepo,IBaseRepository<LeaveModel>leaveRepo,IBaseRepository<RoleModel>roleRepo,IBaseRepository<UserRoleModel>userroleRepo)
        {
            this.accountRepo = accountRepo;
            this.deptRepo = deptRepo;
            this.leaveRepo = leaveRepo;
            this.roleRepo = roleRepo;
            this.userroleRepo = userroleRepo;
        }
        public Task<ApiPaging<ApprovalDTO>> Handle(GetApprovalCommand request, CancellationToken cancellationToken)
        {
           var res=new ApiPaging<ApprovalDTO>();
            var ridlist=request.RoleId.Split(',').Select(x=>Convert.ToInt64(x)).ToList();
            var excuselist=leaveRepo.GetAll().ToList();
            if (ridlist.Contains(1007))
            {
                excuselist= excuselist.Where(x=>x.Bid==request.Bid).ToList();
            }
            else
            {
                excuselist= excuselist.Where(x=>x.Uid==request.Id).ToList();
            }
            var linq = from a in excuselist
                       join b in deptRepo.GetAll()
                       on a.Bid equals b.Bid
                       where a.IsDel == false
                       select new ApprovalDTO
                       {
                            LeaveType    =a.LeaveType   ,
                            BeginTime    =a.BeginTime   ,
                            OverTime     =a.OverTime    ,
                            BaoDaoTime   =a.BaoDaoTime  ,
                            Deptlingdao  =a.Deptlingdao ,
                            ShenpiState  =a.ShenpiState ,
                            DeptName     =b.DeptName    ,
                            CreateTime   =a.CreateTime  ,
                            Name         =a. Name       ,
                       };
            if (!string.IsNullOrEmpty(request.Name))
            {
                linq = linq.Where(x => x.Name.Contains(request.Name)).ToList();
            }
            if (request.BeginTime != null)
            {
                linq = linq.Where(x => x.BeginTime <= request.BeginTime).ToList();
            }
            if (request.OverTime != null)
            {
                linq = linq.Where(x => x.OverTime >= request.OverTime).ToList();
            }
            if (request.ShenpiState != null)
            {
                linq = linq.Where(x => x.ShenpiState == request.ShenpiState).ToList();
            }
            var totalcount = linq.Count();
            var totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.PageSize);
            linq = linq.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            res.totalcount = totalcount;
            res.totalpage = totalpage;
            res.Message = "显示成功";
            res.Data = linq.ToList();
            res.Code = ApiEnum.Ok;
            return Task.FromResult(res);
        }
    }
}
